using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;

namespace Hutchinson.CustomField.Multichoice.MCCustomizable
{
    public class MCCustomizableControlBase : BaseFieldControl
    {

        protected MCCustomizableField _field;

        protected override string DefaultTemplateName
        {
            get
            {
                _field = Field as MCCustomizableField;
                return _field.ShowType.ToString();
            }
        }

        protected override void CreateChildControls()
        {

            if (Field == null)
                return;

            base.CreateChildControls();

            if (ControlMode == SPControlMode.Display)
                return;

            _field = Field as MCCustomizableField;
            if (_field == null)
                return;

        }

        protected void CheckDefaultValue(MCCustomizableField _field, ListControl obj)
        {
            if (_field.DefaultValue != string.Empty && _field.DefaultValue != null)
            {
                string[] values2Check = _field.DefaultValue.Split(';');
                foreach (string s in values2Check)
                    if (obj.Items.FindByText(s) != null)
                        obj.Items.FindByText(s).Selected = true;
                    else
                        obj.Items.FindByText(s).Selected = false;
            }
        }

        public override void Validate()
        {
            base.Validate();

            if (ControlMode == SPControlMode.Display || !IsValid)
                return;

            if (Field.Required && (Value == null || Value.ToString().Length == 0))
            {
                this.ErrorMessage = System.Web.HttpContext.GetGlobalResourceObject("wss", "form_empty_value").ToString();
                IsValid = false;
            }
        }

    }
}
